package crane.bomb;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 
 * @author crane
 *
 */
public class ModalWindow extends JDialog{
    
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -320148248646763516L;

    /**
     * Constructor
     * 
     * @param frame
     * @param title
     * @param message
     */
    public ModalWindow(JFrame frame, String title, String message){
        super(frame, true);
        try{
            this.setResizable(false);
            this.setTitle(title);
            Container cp = this.getContentPane();
            cp.setLayout(new BorderLayout());
            this.addWindowListener(new ActionAdapter(){
                public void windowClosing(WindowEvent we) {
                    ModalWindow.this.dispose();
                }
            });
            cp.setBounds(400, 250, 260, 200);
            cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
            JPanel panel = new JPanel();
            JLabel nullLabel = new JLabel();
            panel.add(nullLabel);
            cp.add(panel, BorderLayout.NORTH);
            
            
            panel = new JPanel();
            JLabel imageLabel = new JLabel();
            imageLabel.setIcon(FileUtil.getImage("/images/alarm.gif"));
            panel.add(imageLabel);
            
            String[] msg  = message.split("\n");
            int len = msg.length;
            JLabel label = new JLabel();
            JPanel subPanel = new JPanel();
            subPanel.setLayout(new GridLayout(len, 1, 2, 2));
            for (int i = 0; i < len; i++) {
                label = new JLabel(msg[i]);
                subPanel.add(label);
            }
            subPanel.setOpaque(true);
            panel.add(subPanel);
            cp.add(panel);
            cp.add(Box.createVerticalStrut(30));

            panel = new JPanel();
            JButton btn_OK = new JButton("OK");
            btn_OK.setFocusable(true);
            panel.add(btn_OK);

            btn_OK.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                    ModalWindow.this.dispose();
                }
            });
            cp.add(panel);
            cp.setVisible(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
